/***************************************************************************
 *   Copyright (C) 2006 by Matej Svejda                                    *
 *   mata@aw-modell.at                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 
#include "sdlbeat.h"
#include "sdlbeat.moc"


SDLBeat::SDLBeat(QString normal, QString accentuated, QObject *parent, const char *name) :
	Beat(parent, name)
{
	// Either normal has to be an absolute path or normal has to contain the name of the sound  located in the destick datadir (then accentuated is ignore)d. If normal is an absolute path and accentuated isn't specified it gets the same value as normal.
	
	available = true;
	
	if(normal.at(0) != '/') {
		accentuated = locate("data", "destick/" + normal + "_accentuated.wav");
		normal = locate("data", "destick/" + normal + "_normal.wav");
	} else if(accentuated == "") {
		accentuated = normal;
	}
	
	if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1) {
		available = false;
	}
	
	normalChunk = Mix_LoadWAV(normal);
	accentuatedChunk = Mix_LoadWAV(accentuated);
	
	if(normalChunk == NULL || accentuatedChunk == NULL) {
		available = false;
	}
}

SDLBeat::~SDLBeat()
{
	
}

void SDLBeat::makeSound()
{
	if(available) {
		Mix_PlayChannel(-1, normalChunk, 0);
	}
}

void SDLBeat::makeAccentuatedSound()
{
	if(available) {
		Mix_PlayChannel(-1, accentuatedChunk, 0);
	}
}

void SDLBeat::setVolume(float volume)
{
	Mix_Volume(-1, static_cast<int>(MIX_MAX_VOLUME * volume));
}

